// @ts-ignore
import { ElMessage } from 'element-plus/es'
// @ts-ignore
import { Message } from '@arco-design/web-vue'
import { sendValidationCode } from '~/server/userAuth.service'

export class CommonService {
  private static _ins: CommonService = new CommonService()

  public static get ins(): CommonService {
    return this._ins ? this._ins : (this._ins = new CommonService())
  }

  /** 下发邮箱验证码 */
  public sendEmailCode(email: string): Promise<void> {
    return new Promise((resolve, reject) => {
      if (!/\S+@\S+\.\S+/.test(email)) {
        Message.error('请正确输入邮箱')
        reject()
        return
      }
      sendValidationCode(email).then(({ data }:any) => {
        if (data.flag) {
          ElMessage({ message: '验证码已发送', type: 'success' })
          resolve()
        } else {
          ElMessage({ message: data.message, type: 'error' })
          reject(data.message)
        }
      })
    })
  }

  public verify(email: string, code: string): Promise<string> {
    return new Promise((resolve) => {
      if (email && code) {
        resolve('Verify Success')
      } else {
        Message.error('请先输入邮箱或验证码')
        resolve('')
      }
    })
  }
}

export const commonService: CommonService = CommonService.ins
